﻿using System.Collections;
using System.Collections.Generic;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.IO;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public struct PrintParamater
    {
        public string DocType;
        public string PrintType;
        public DataSet Set;
        public string strExcelFile;
        public string Printer;
    }
    public partial class ITXEXRPT : BaseReport
    {
        public readonly static ITXEXRPT Instance = new ITXEXRPT();
        Queue<PrintParamater> PrintQueue = new Queue<WPFExcelReport.PrintParamater>();
        bool IsRun = false;
        bool InstanceMode = false;
        public void AutoRun()
        {
            if (IsRun)
                return;
            IsRun = true;
            while (true)
            {
                if (PrintQueue.Count == 0)
                    break;
                PrintParamater PP = PrintQueue.Dequeue();
                DocType = PP.DocType;
                dsSource = PP.Set;
                Printer = PP.Printer;
                strExcelFile = PP.strExcelFile;
                WriteExcelReport(PP.PrintType);
            }
            IsRun = false;
        }
        public void Print(PrintParamater PP)
        {
            InstanceMode = true;
            PrintQueue.Enqueue(PP);
            AutoRun();
        }
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        VHRSTAMM _VHR;
        public VHRSTAMM VHR
        {
            get
            {
                if (_VHR == null)
                    _VHR = new VHRSTAMM();
                return _VHR;
            }
        }
        public Dictionary<string, object> ENVMAIN
        {
            get
            {
                return app.ENVMAIN(app.UserIP);
            }
        }
        public VICC_LOG VLOG
        {
            get
            {
                if (!ENVMAIN.ContainsKey("VLOG"))
                    ENVMAIN["VLOG"] = new VICC_LOG();
                return (VICC_LOG)ENVMAIN["VLOG"];
            }
        }
        public bool IsApplyPayment = false;
        public string DocType = "";
        public string Printer = "";
        public string DestFile = "";
        public string prefix = "";
        public string STARTD = "", ENDD = "";
        public int COPYS = 1;
        public List<string> Standards;
        public string Shareroot = @"R:\";
        public string LocalRoot = @"C:\IntexReport";
        public DataSet SetExchange = null;
        DataTable tbExport = null;
        public ITXEXRPT()
            : base()
        {
            tbExport = CreateTbExport();
            Root = "C:\\IntexReport";
            strCompany = app.company;
            PrintType = "OpenExcel";
            dsSource = new DataSet();
        }

        private DataTable CreateTbExport()
        {
            DataTable tb = new DataTable();
            for (int i = 1; i < 100; i++)
            {
                string field = "F" + Right("00" + i, 2);
                tb.Columns.Add(field, typeof(System.String));
            }
            return tb;
        }
        public virtual void SetUpTemplate()
        {  
            TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\" + strExcelFile);
            if (DocType == "TSTEXAPP" || DocType == "AFSEXAPP" || DocType == "AFSEXAPP1" || DocType == "AFSEXAPP3")
            {
                TemplatePath = @"C:\IntexReport\Report\" + strExcelFile;
            }
            exAgent.Company = strCompany;
            exAgent.strExcelFile = strExcelFile;
            exAgent.HideApp();
            if (DocType != "JLTWBD10IT")
            {

                if (System.IO.File.Exists(TemplatePath))
                {
                    exAgent.Open(TemplatePath);
                }
            }
        }
        string order = "";
        string KRS = "";
        string LFD = "";
        string POS = "";
        DataRow dwOrder = null;
        string tempfile = "";
        string pwd = "";
        string temp = "tmp";// System.DateTime.Now.ToFileTime().ToString();
        public string ExecutePrint( ref bool IsReturn)
        {
            filename = AFSPrint(ref IsReturn);
            filename = ITXPrint(ref IsReturn);
            filename = JLTPrint(ref IsReturn);
            filename = YGRPrint(ref IsReturn);
            filename = YCAPrint(ref IsReturn);
            filename = FLTPrint(ref IsReturn);
            filename = TSTPrint(ref IsReturn);
            filename = SYTPrint(ref IsReturn);
            filename = UBTPrint(ref IsReturn);
            filename = YCCPrint(ref IsReturn);
            filename = SYHPrint(ref IsReturn);
            filename = SJHPrint(ref IsReturn);
            
            return filename;
        }
        public void ExecuteTemplate()
        {
            AFSTemplate();
            FLTTemplate();
            ITXTemplate();
            JLTTemplate();
            UBTTemplate();
            TSTTemplate();
            YGRTemplate();
            SYHTemplate();
            SJHTemplate();
            SetUpTemplate(); 
        }

        public string WriteExcelReport(string _PrintType = "OpenExcel")
        {
            PrintType = _PrintType;
            Standards = new List<string>();
          
            try
            {
                ExecuteTemplate();
                bool IsReturn = false;
                ExecutePrint( ref IsReturn);
                if (IsReturn)
                    return filename; 
                exAgent.Save();
                exAgent.Close();
                ServerShareFile(tempfile);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                try
                {
                    if (!InstanceMode)
                    {
                        exAgent = null;
                    }
                    exAgent = null;
                    if (File.Exists(tempfile))
                    {
                        File.Delete(tempfile);
                    }
                }
                catch (Exception X)
                { }
            }
            return DestFile;
        }
        private void DirectPrint(string tmpfile, int copys = 1)
        {
            exAgent.PrintOut("", 0, copys, false, Printer);
        }
        private void ServerShareFile(string tempfile)
        {
            if (tempfile == "")
                return;

            if (File.Exists(DestFile))
            {
                File.Delete(DestFile);
            }
            File.Move(tempfile, DestFile);
            BackUp(dsSource);
            BackUpFB(dsSource);
            if (PrintType == "OpenExcel")
            {
                System.Diagnostics.Process.Start("EXCEL.EXE", DestFile);
            }
            if (PrintType == "PrintEX")
            {
                //System.Diagnostics.Process.Start("EXCEL.EXE", DestFile);
            }
        }
    }
}
